package com.bawei.module_my.log;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.library_base.Api;
import com.bawei.library_base.RouterPath;
import com.bawei.library_base.entity.RegLogUser;
import com.bawei.library_base.entity.User;
import com.bawei.library_base.manager.MyManager;
import com.bawei.library_base.mvp.v.BaseActivity;
import com.bawei.module_my.R;
import com.blankj.utilcode.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

@Route(path = RouterPath.Log.LOG)
public class LogActivity extends BaseActivity {


    private TextView goReg;
    private EditText mMobileEt;
    private EditText mPwdEt;
    private Button mLoginBtn;
    private TextView mForgetPwdTv;
    private TextView outLog;

    @Override
    public int beanLayout() {
        return R.layout.activity_log;
    }

    @Override
    public void initView() {

        goReg = (TextView) findViewById(R.id.go_reg);
        mMobileEt = (EditText) findViewById(R.id.mMobileEt);
        mPwdEt = (EditText) findViewById(R.id.mPwdEt);
        mLoginBtn = (Button) findViewById(R.id.mLoginBtn);
        mForgetPwdTv = (TextView) findViewById(R.id.mForgetPwdTv);
        outLog = (TextView) findViewById(R.id.out_log);
    }

    @Override
    public void initData() {

        //去注册页
        goreg();

        //退出登录页
        outlog();

        //登录
        golog();

    }

    private void golog() {
        mLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyManager.getInstance().getRetrofit("http://82.156.178.182:8082/")
                        .create(Api.class)
                        .log(new RegLogUser(mMobileEt.getText().toString(),mPwdEt.getText().toString()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<User>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public  void onNext(@NotNull User user) {
                                if (user.getCode()==200){
                                    ToastUtils.showShort(user.getMsg());

                                    EventBus.getDefault().post("golog");
                                    finish();

                                }else if (user.getCode()==-1){
                                    Log.i("66666666TAG", "onNext: "+user.getMsg());
                                    ToastUtils.showShort(user.getMsg());
                                }else {
                                    Log.i("66666666TAG", "onNext: "+user.getMsg());
                                }
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }

    private void outlog() {
        outLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void goreg() {
        //跳转到注册
        goReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build(RouterPath.Reg.REG)
                        .navigation();
            }
        });

    }
}